﻿using loginWinForm.common;
using loginWinForm.entity;
using loginWinForm.service;
using loginWinForm.service.impl;
using System;
using System.Windows.Forms;

namespace loginWinForm.form
{
    public partial class Register : Form
    {
        ILoginService loginService = new LoginService();
        public Register()
        {
            InitializeComponent();
        }

        private void regTj_Click(object sender, EventArgs e)
        {
            string pwd2 = regPwd2.Text;
            // 初始化 user 对象
            SysUser user = new SysUser(System.Guid.NewGuid().ToString("N"), regName.Text, regPwd.Text, regMc.Text);

            if (user.Name.Length <= 0)
            {
                ResultBox.error("请输入用户名", "注册提示");
                return;
            }
            if (user.Password.Length <= 0)
            {
                ResultBox.error("请输入密码", "注册提示");
                return;
            }
            if (pwd2.Length <= 0)
            {
                ResultBox.error("请确认密码", "注册提示");
                return;
            }
            if (user.Mc.Length <= 0)
            {
                ResultBox.error("请输入名称", "注册提示");
                return;
            }
            if (!user.Password.Equals(pwd2))
            {
                ResultBox.error("2次密码输入不一致，请重新输入", "注册提示");
                regPwd.Text = "";
                regPwd2.Text = "";
                return;
            }
            try
            {
                loginService.saveUser(user);
                ResultBox.ok("注册成功", "注册提示");
                this.Hide();
                new login().Show();
            }
            catch (Exception E)
            {
               ResultBox.error("注册失败，请稍后重试", "注册提示");
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            new login().Show();
        }
    }
}
